package cn.huiyunche.base.service.interfaces.impl;

import cn.huiyunche.base.service.interfaces.BDealerDistributaryService;
import cn.huiyunche.base.service.mappers.BDealerDistributaryMapper;
import cn.huiyunche.base.service.model.BDealerDistributary;
import cn.huiyunche.base.service.model.BDealerDistributaryExample;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @FileName: cn.huiyunche.service.base.tms.interfaces.impl
 * @Description: Description
 * @author: Aaron
 * @date: 2016/11/2 下午1:26
 */
@Service
public class BDealerDistributaryServiceImpl implements BDealerDistributaryService {

    private static final Logger LOGGER = LoggerFactory.getLogger(BDealerDistributaryServiceImpl.class);

    @Autowired
    private BDealerDistributaryMapper bDealerDistributaryMapper;

    @Override
    public Integer getIdByConditions(Integer dealerId, String dealerDistributaryCode, String dealerDistributaryName) {
        LOGGER.info("getIdByConditions params : {}, {}, {}", dealerId, dealerDistributaryCode, dealerDistributaryName);
        if (null == dealerId) {
            LOGGER.error("getIdByConditions param dealerId must not null");
            throw new IllegalArgumentException("经销商主键不能为空");
        }
        if (StringUtils.isBlank(dealerDistributaryCode)) {
            LOGGER.error("getIdByConditions param dealerDistributaryCode must not null");
            throw new IllegalArgumentException("经销商收货点编号不能为空");
        }
        if (StringUtils.isBlank(dealerDistributaryName)) {
            LOGGER.error("getIdByConditions param dealerDistributaryName must not null");
            throw new IllegalArgumentException("经销商收货点名称不能为空");
        }

        BDealerDistributaryExample example1 = new BDealerDistributaryExample();
        example1.createCriteria().andDealerIdEqualTo(dealerId).andDistributaryCodeEqualTo(dealerDistributaryCode).andDistributaryNameEqualTo(dealerDistributaryName);

        BDealerDistributaryExample example2 = new BDealerDistributaryExample();
        example2.createCriteria().andDealerIdEqualTo(dealerId).andDistributaryCodeEqualTo(dealerDistributaryCode);

        List<BDealerDistributary> list = bDealerDistributaryMapper.selectByExample(example1);
        if (CollectionUtils.isEmpty(list)) {
            list = bDealerDistributaryMapper.selectByExample(example2);
            if (CollectionUtils.isNotEmpty(list)) {
                BDealerDistributary distributary = list.get(0);
                distributary.setDistributaryName(dealerDistributaryName);
                bDealerDistributaryMapper.updateByPrimaryKeySelective(distributary);
                return distributary.getId();
            }
        } else {
            return list.get(0).getId();
        }

        BDealerDistributary distributary = new BDealerDistributary();
        distributary.setDealerId(dealerId);
        distributary.setDistributaryCode(dealerDistributaryCode);
        distributary.setDistributaryName(dealerDistributaryName);
        bDealerDistributaryMapper.insertSelective(distributary);

        return distributary.getId();
    }
}
